﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using K2CoreLib.DataObject;
using K2CoreLib.Dao.Sys;

namespace K2CoreLib.DataProvider
{
    public class MailAction
    {
        // Get mail notify information
        public MailDto GetMailNotify(string ticketName, string processCode, string nextApprover, string historyUrl, string description)
        {
            object[] parms = {"$TicketName$", ticketName,
                             "$Code$", processCode,
                             "$NextApprover$", nextApprover,
                             "$url$", historyUrl,
                             "$ContentTicket$", description};
            MailDto result = GetMailContent("K2-Notify", "K2-Signature", parms);
            return result;
        }

        // Get mail for approver to alert process waiting for approve (approve by mail)
        public MailDto GetMailWaitingApproveByMail(string ticketName, string processCode, string requester, string approveUrl, string description)
        {
            object[] parms = {"$TicketName$", ticketName,
                             "$Code$", processCode,
                             "$Requester$", requester,
                             "$url$", approveUrl,
                             "$ContentTicket$", description};
            MailDto result = GetMailContent("K2-Waiting", "K2-Signature", parms);
            return result;
        }

        // Get mail for approver to alert process waiting for approve (no approve by mail)
        public MailDto GetMailWaiting(string ticketName, string processCode, string requester, string approveUrl, string description)
        {
            object[] parms = {"$TicketName$", ticketName,
                             "$Code$", processCode,
                             "$Requester$", requester,
                             "$url$", approveUrl,
                             "$ContentTicket$", description};
            MailDto result = GetMailContent("K2-Waiting-NoApproveByMail", "K2-Signature", parms);
            return result;
        }

        // Get mail alert process finished
        public MailDto GetMailFinished(string ticketName, string processCode, string historyUrl, string description)
        {
            object[] parms = {"$TicketName$", ticketName,
                             "$Code$", processCode,
                             "$url$", historyUrl,
                             "$ContentTicket$", description};
            MailDto result = GetMailContent("K2-Finished", "K2-Signature", parms);
            return result;
        }

        // Get mail content
        // parms: array object will be replace 
        public MailDto GetMailContent(string mailcode, string signature, object[] parms = null)
        {
            MailDao dao = new MailDao();
            MailTemplateDTO maildto = dao.GetMailWithSignature(mailcode, signature);
            string subject = maildto.TitleEN;
            string content = maildto.ContentVN;
            
            // Replace parameter in mail template
            if (parms != null && parms.Length > 0)
            {
                // NOTE: Processes a name/value pair at each iteration
                for (int i = 0; i < parms.Length; i += 2)
                {
                    string parameter = parms[i].ToString();
                    string value = parms[i + 1].ToString();

                    subject = subject.Replace(parameter, value);
                    content = content.Replace(parameter, value);
                }
            }

            MailDto mail = new MailDto();
            mail.Subject = subject;
            mail.Content = content;

            return mail;
        }

    }
}
